/*
 * BinderMethod.scala
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.liftweb.widgets.trees.util

import scala.collection.mutable.HashMap
import scala.xml.{NodeSeq, Node, PCData, Text, Unparsed, NodeBuffer, Elem}

import java.lang.reflect.Method

object BinderMethod {
    // TOOD: search a better key
  def rowTypeKey (clazz : Class[_]) = {
    val classname = clazz.getCanonicalName
      
    classname.substring (classname.lastIndexOf ('.') + 1)
  }
}

class BinderMethod (rowsBinder : Function2[NodeSeq, Nothing, NodeSeq]*) {
  // map the type of row with the "row binder" object and method
  var mapBinderMethod = new HashMap [String, Pair [Function2[NodeSeq, Nothing, NodeSeq], Method]] ()

  // the row-type class is the second parameter of the render method
  for (rowBinder <- rowsBinder; 
           method <- rowBinder.getClass.getMethods 
             if (method.getName == "apply") &&
               method.getParameterTypes.length == 2 &&
               method.getParameterTypes.apply (0).getName != "java.lang.Object"
           )
      mapBinderMethod += (BinderMethod.rowTypeKey (method.getParameterTypes.apply (1)) -> (rowBinder, method))
        
  println (mapBinderMethod)
  
  /**
   *  search the method that takes a row and a template as input and returns a xhtml
   */

  def get (rowtype : String) = {
    mapBinderMethod (rowtype)
  }
  
}
